package com.shchuangzhi.smartguard.core;

import android.content.Context;
import com.shchuangzhi.smartguard.R;

/**
 * Created by j_hu on 4/17/17.
 */

public class User{
    private String account;
    private boolean isAdmin;
    private Context ctx;
    private DBHelper helper;

    private static User instance = null;
    private static final int ROLE_ADMIN = 1;

    private User(Context ctx) {
        this.ctx = ctx;
        this.helper = new DBHelper(ctx);
    }

    public String getAccount() {
        return account;
    }

    public boolean isAdmin() {
        return this.isAdmin;
    }

    public LoginResult Login(String account, String pwd) {
        int roleId = helper.getRole(account, pwd);
        if (roleId != -1) {
            this.account = account;
            this.isAdmin = roleId == ROLE_ADMIN;
            return new LoginResult(true, "");
        } else {
            return new LoginResult(false, ctx.getString(R.string.login_error));
        }
    }

    public static User create(Context ctx) {
        instance = new User(ctx);
        return get();
    }

    public static User get() {
        return instance;
    }

    public class LoginResult {
        private boolean ok;
        private String message;

        public LoginResult(boolean ok, String message) {
            this.ok = ok;
            this.message = message;
        }

        public boolean OK() {
            return ok;
        }

        public String Message() {
            return message;
        }
    }
}
